﻿@{
    ViewBag.Title = "Form";
    Layout = "~/Views/Shared/_ManufactureList.cshtml";
}
<style type="text/css">
    .ui-jqgrid .ui-jqgrid-pager .ui-pager-table,
    .ui-jqgrid .ui-jqgrid-toppager .ui-pager-table {
        width: 95%;
    }
    a.choose_mold {
        font-weight: bold;
        text-decoration: underline;
    }
</style>
<script type="text/javascript">
    var status = @{<text>ViewBag.status</text>};
    $(function () {
        initControl();
        gridList();
    });
    function initControl() {
        //$('select').bindSelect();
    }
    function gridList() {
        var $gridList = $("#gridList");
        $gridList.dataGrid({
            url: "/BOM/Manufacture/GetMakeLineJson",
            height: 198,
            colModel: [
                {
                    label: '选择', name: 'MakeLineNO', width: 90, align: 'center', sortable: false, frozen: true, formatter: function (cellvalue) {
                        return '<a id="" class="choose_mold" href="javascript:;" onclick="do_choose_makeline(\'' + cellvalue + '\')" style="width:45px;">选择</a> &nbsp;<a id="" class="choose_mold" href="javascript:;" onclick="cancel_choose_makeline(\'' + cellvalue + '\')" style="width:45px;">取消</a>';
                    }
                },
                { label: '路线编码', name: 'MakeLineNO', width: 70, align: 'center', sortable: false },
                { label: '路线名称', name: 'MakeLineShortName', width: 120, align: 'left', sortable: false },
                { label: '解释说明', name: 'MakeExplain', width: 200, align: 'left', sortable: false },
            ],
            pager: "#gridPager",
            sortname: 'MakeLineNO asc',
            viewrecords: true,
            rowNum: 10000000, //每页显示记录数
            rownumWidth: 40,
            rowList: [100, 200, 300], //可调整每页显示的记录数   
            ondblClickRow: function (rowid) {
                var rowData = $gridList.jqGrid('getRowData', rowid);
                do_choose_makeline(rowData.MakeLineNO);
                
            }
        });
        $gridList.jqGrid('setLabel', 0, '序号', 'rowNumberHeaderStyle');
        $("#btn_search").click(function () {
            $gridList.jqGrid('setGridParam', {
                postData: { keyword: $("#txt_keyword").val() },
            }).trigger('reloadGrid');
        });
    }
    function do_choose_makeline(number) {
        if (status == 0) {
            $('#txt_result').val(number);
        } else {
            var line = $('#txt_result').val();
            if (line == "") {
                $('#txt_result').val(number);
            } else {
                var array = line.split("/");
                if (array.length > 0 && array[array.length - 1] != number) {
                    //$('#txt_result').val(line + "/" + number);
                    array.push(number);
                    $('#txt_result').val(array.join("/"));
                }
            }
        }
    }
    function cancel_choose_makeline(number){
        if (status == 0) {
            $('#txt_result').val("");
        } else {
            var line = $('#txt_result').val();
            if (line != "") {
                var array = line.split("/");
                if (array.length > 0 && array[array.length - 1] == number) {
                    array.splice(array.length - 1, 1);
                    $('#txt_result').val(array.join("/"));
                }
            }
        }
    }
    function btn_ok(){
        var recordArray = [];
        var linecount = $('#gridList').jqGrid('getGridParam', 'reccount');
        for (i = 1; i <= linecount; i++) {
            var rowData = $('#gridList').jqGrid('getRowData', i);
            recordArray.push(rowData);
        }
        //alert(recordArray.length);

        var line = $('#txt_result').val();
        var flag = true;
        if (line != ""){
            var array = line.split("/");
            var arrayText = [];
            for (i = 0; i < array.length; i++) {
                var index = findElement(recordArray, "MakeLineNO", array[i]);
                if (index < 0) {
                    flag = false;
                    break;
                }
                else {
                    arrayText.push(recordArray[index].MakeLineShortName);
                }
            }
        }else{
            var array = [];
            var arrayText = [];
        }
        if (!flag) {
            $.modalAlert("已选路线中有错误的编码，请检查！", "error");
        } else {
            var father = $.currentSubSubWindow().frames["Detail"];
            var number = array.join('/');
            var text = arrayText.join('/');
            if (status == 0) {
                father.do_choose_assembleline(number, text);
            } else {
                father.do_choose_makeline(number, text);
            }
        }
    }
    function findElement(arrayToSearch, attr, val) {
        for (var i = 0; i < arrayToSearch.length; i++) {
            if (arrayToSearch[i][attr] == val) {
                return i;
            }
        }
        return -1;
    }
</script>
<div id="layout" style="height: 100%; width: 100%; ">
    <div style="padding: 8px 0 5px 0; padding-left: 15px; border-bottom: 1px solid #ccc; " class="line-table input-width-320">
        <label class="prefix">已选路线</label> <input id="txt_result" type="text" class="form-control" value="@ViewBag.result" />
        <div class="btn-group" style="">
            <a id="btn_ok" class="btn btn-primary center-block btn-1" onclick="btn_ok()"><span class="glyphicon glyphicon-ok"></span> 确定</a>
        </div>
    </div>
    <div style="padding: 5px 0 5px 0; padding-left: 15px; border-bottom: 1px solid #ccc; " class="line-table input-width-185">
        <label class="prefix">路线编码</label> <input id="txt_keyword" type="text" class="form-control" />
        <div class="btn-group" style="">
            <a id="btn_search" class="btn btn-primary center-block btn-1" onclick=""><span class="glyphicon glyphicon-search"></span> 检索</a>
        </div>
    </div>
    <div>
        <table id="gridList"></table>
    </div>
</div>

